local sk__fangtong = fk.CreateSkill {

  name = "sk__fangtong",

  tags = { Skill.Compulsory, },

}



sk__fangtong:addEffect(fk.EventPhaseStart, {
  name = "sk__fangtong",
  anim_type = "drawcard",
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(sk__fangtong.name) and player.phase == Player.Finish then
      local n = 0
      for _, id in ipairs(player:getPile("sk__zhangliang__bing")) do
        n = n + Fk:getCardById(id).number
      end
      n = math.min(n // 9, 4)
      if player:getHandcardNum() < n then
        event:setCostData(self, n - player:getHandcardNum())
        return true
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    player:drawCards(event:getCostData(self), sk__fangtong.name)
  end,
})
sk__fangtong:addEffect(fk.AfterCardsMove, {
  can_refresh = function (self, event, target, player, data)
    for _, move in ipairs(data) do
      if move.to and move.to == player and move.toArea == Card.PlayerSpecial and move.specialName == "sk__zhangliang__bing" then
        return true
      end
      if move.from and move.from == player then
        for _, info in ipairs(move.moveInfo) do
          if info.fromArea == Card.PlayerSpecial and info.fromSpecialName == "sk__zhangliang__bing" then
            return true
          end
        end
      end
    end
  end,
  on_refresh = function (self, event, target, player, data)
    local n = 0
    for _, id in ipairs(player:getPile("sk__zhangliang__bing")) do
      n = n + Fk:getCardById(id).number
    end
    n = math.min(n // 9, 4)
    local all_skills = {"leiji", "sk__zhoufu", "sk__shendao", "sk__biantian"}
    local mark = player:getTableMark(sk__fangtong.name)
    local skills = {}
    for i = 1, 4, 1 do
      local s = all_skills[i]
      if i <= n and not player:hasSkill(s, true) and not table.contains(mark, s) then
        table.insert(skills, s)
        table.insert(mark, s)
      elseif i > n and player:hasSkill(s, true) and table.contains(mark, s) then
        table.insert(skills, "-"..s)
        table.removeOne(mark, s)
      end
    end
    player.room:setPlayerMark(player, sk__fangtong.name, mark)
    player.room:handleAddLoseSkills(player, table.concat(skills, "|"), nil, false, true)
  end,
})

return sk__fangtong